package com.fan.teamer.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.transaction.annotation.Transactional;

import com.fan.teamer.model.dao.MemberDao;
import com.fan.teamer.model.dao.TeamDao;
import com.fan.teamer.model.dao.TeamMemberDao;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.model.domain.TeamMember;
import com.fan.teamer.model.domain.TeamMemberId;

@Named("teamMemberService")
@Transactional(readOnly = true)
public class TeamMemberServiceImpl implements TeamMemberService {

	@Inject
	@Named("teamMemberDao")
	private TeamMemberDao teamMemberDao;

	@Inject
	@Named("memberDao")
	private MemberDao memberDao;

	@Inject
	@Named("teamDao")
	private TeamDao teamDao;

	public TeamMemberDao getTeamMemberDao() {
		return teamMemberDao;
	}

	public void setTeamMemberDao(TeamMemberDao teamMemberDao) {
		this.teamMemberDao = teamMemberDao;
	}

	public MemberDao getMemberDao() {
		return memberDao;
	}

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

	public TeamDao getTeamDao() {
		return teamDao;
	}

	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getList() {
		return teamMemberDao.getAllTeamMembers();
	}

	@Override
	public int getColumnsSize() {
		return teamMemberDao.getColumnNames().size();
	}

	@Override
	public Object copy(Object obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TeamMember getTeamMember(TeamMemberId id) {
		return teamMemberDao.getTeamMember(id);
	}

	@Override
	public List<TeamMember> getAllTeamMembers() {
		return teamMemberDao.getAllTeamMembers();
	}

	@Override
	public int getSize() {
		return teamMemberDao.getSize();
	}

	@Override
	@Transactional(readOnly = false)
	public void addTeamMember(TeamMember teamMember) {
		teamMemberDao.addTeamMember(teamMember);
	}

	@Override
	@Transactional(readOnly = false)
	public void updateTeamMember(TeamMember teamMember) {
		teamMemberDao.updateTeamMember(teamMember);
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteTeamMember(TeamMember teamMember) {
		teamMemberDao.deleteTeamMember(teamMember);
	}

	@Override
	public boolean hasTeamMember(TeamMemberId id) {
		return teamMemberDao.hasTeamMember(id);
	}

	@Override
	public List<String> getColumnNames() {
		return teamMemberDao.getColumnNames();
	}

	@Override
	public List<Member> getTeamMembers(Team team) {
		List<Member> members = new ArrayList<Member>();
		for (String email : teamMemberDao.getMembersEmailListByTeam(team)) {
			members.add(memberDao.getMember(email));
		}
		return members;
	}

	@Override
	public List<Team> getMemberTeams(Member member) {
		List<Team> teams = new ArrayList<Team>();
		for (int id : teamMemberDao.getTeamsIdListByMember(member)) {
			teams.add(teamDao.getTeam(id));
		}
		return teams;
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteByMember(Member member) {
		teamMemberDao.deleteByMember(member);
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteByTeam(Team team) {
		teamMemberDao.deleteByTeam(team);
	}

	@Override
	public TeamMember getTeamMember(Team team, Member member) {
		return teamMemberDao.getTeamMember(team, member);
	}

	@Transactional(readOnly = false)
	@Override
	public void deleteMemberFromTeam(Team team, Member member) {
		TeamMember teamMember = teamMemberDao.getTeamMember(team, member);
		if (teamMember != null) {
			teamMemberDao.deleteTeamMember(teamMember);
		}

	}

}
